#ifndef __STATE_MACHINE_H
#define __STATE_MACHINE_H

#include "motor.h"
#define Motor_StandBy		(int8_t)0
#define NonlinerFlux 	  (int8_t)1
#define EncoderMode			(int8_t)2
#define Wind_time_test  (int8_t)3
#define Wind            (int8_t)4
#define test            (int8_t)5

typedef enum {
    IDLE = 0,
    ROTOR_ID = 1,     /* Rotor position identification*/
    IF_START = 2,     /* IF drag phase */
    START_SWITCH = 3, /* from IF drag stage(current closed loop and speed open loop) change to speed and current double closed loop*/
    SWITCH_OVER = 4,  /*  open loop to closed loop switch completed.*/
    RUN = 5,          /* Persistent state with running motor. */
		Pusle = 6,      
    DelayTime = 7,  
		SecondPulse=8,
		Calc_Est=9,
    STOP =10,
		Judgement=11,
		Pusle_TEST=12,
		IDLE_short=13,
		IDLE_WIND=14,
		wind_handle=15,
} State_t;

extern State_t Motor_state;

typedef struct
{
	int16_t RefSpeed;
	int16_t CommandSpeed;
	int16_t RefSpeedFilter;
	int16_t DirChangeFlag;

	int16_t OCflag;
	int16_t DCmode_flag;
	int16_t DCmode_cnt;

	int16_t MotorAngle;
	int16_t MotorSpeed;
	int16_t VoltAngle;
		
	int16_t VoltAlpha;
	int16_t VoltBeta;
	
	int16_t TsPU;

	int16_t FluxPu;
	
	int16_t IdRef;
	
	int16_t IdFilter;
	int16_t IqFilter;
	
	int16_t StateMachineFlag;
	
	int16_t BUS_Coef;

	int16_t CurrFirstAD;
	int16_t CurrSencondAD;
	int16_t CurrThirdAD;
	int16_t IdcAD;
	int16_t DcBusAD;
	
	int16_t Ialpha;
	int16_t Ibeta;
	
	int16_t DcBusFiltered; //Q13
	int16_t DcCurrFiltered;
	
	int16_t FluxInvPu;
			
	int16_t CurrUphase;
	int16_t CurrVphase;
	int16_t CurrWphase;
		
	int16_t CurrOffsetCnt;
	
	int16_t IuOffset;
	int16_t IvOffset;
	int16_t IDC_Offset;
	
	int32_t IuSum;
	int32_t IvSum;
	int32_t IDC_Sum;

	
	int16_t PrePosCnt;


	int16_t Speed_Loop_Kp;
	int16_t Speed_Loop_Ki;
	
	int16_t Speed;
	
	float MecSpeed;
	
	int16_t MecSpeedFdb;
	int8_t Current_correction_flag;
	uint16_t Is;
	int16_t Erro;
	int16_t debug[10];
	float Speed_COEF;
	int16_t Speed_Filter;
	int16_t kalman_filter;
}
SysPara_t;

extern SysPara_t SysPara;

#endif
